<?php
/*
Plugin Name: Admin SSL
Plugin URI: http://www.kerrins.co.uk/blog/admin-ssl/
Description: Secures any WordPress URL using Private or Shared SSL. Once the plugin is activated go to the Admin SSL config page to enable SSL and read the <a href="http://www.kerrins.co.uk/blog/admin-ssl/setup/">installation instructions</a>.
Author: BCG
Version: 1.3
Date: 2008-09-09 09:09
Author URI: http://www.kerrins.co.uk/

For changelog please see release-notes.txt

*/

//
//	if you are experiencing problems, set this to 'true' and messages will be logged
//	in the Admin SSL directory (usually /wp-content/plugins/admin-ssl/debug.log
//

	define("DEBUG",false);

//
//	if this is 'true' then the database will be updated with the default options - use if
//	you cannot access admin-ssl-reset.php after setting wrong Shared SSL settings
//	you MUST change back to false after resetting, or you will not be able to enable SSL
//

	define("RESET",false);

/*																							*
 *																							*
 * 																							*
 * 																							*
 * 			DO NOT EDIT BELOW THIS LINE - USE THE CONFIG PAGE TO CHANGE SETTINGS			*
 * 																							*
 * 																							*
 * 																							*
 * 																							*
 */


//
//	requires $wp_version check - this plugin WILL BREAK earlier versions of WordPress and WPMU
//

	if(isset($wp_version) && strpos($wp_version,"wordpress-mu") === false && $wp_version >= 2.2)
	{
	//
	//
	//	DEBUG MODE AND FUNCTIONS
	//
	//

		if(!defined("TEST") || $first_test):

			require_once("includes/debug.php");
			require_once("includes/functions.php");

		endif;

	//
	//
	//	OPERATING DIRECTORY AND WPMU DETECTION
	//
	//

		//
		//	get operating directory and log environment variables
		//

			$slash = strpos(__FILE__,"/") === false ? "\\" : "/";
			$path = str_replace($slash."admin-ssl.php","",__FILE__);
			$dir = substr($path,strrpos($path,$slash)+1);

			as_log("### ADMIN SSL BEGINS ###");
			as_log("HTTP Host: ".host()."
				Request URI: ".req_uri()."
				Redirect to: ".redirect_to()."
				Found admin-ssl.php in
				 - path: $path
				 - directory: $dir");

		//
		//	if operating directory is mu-plugins, get the name of admin-ssl directory
		//

			$plugins_dir = "plugins";
			$config_page = $wp_version < 2.5 ? "config-page-old.php" : "config-page.php";

			if($dir === "mu-plugins")
			{
				$d_mu_plugins = dir($path);
				$tmp = "";

			//
			//	loop through the main plugins directory
			//

				while(false !== ($plugin_dir = $d_mu_plugins->read()))
				{
					if($plugin_dir !== "." && $plugin_dir !== "..")
					{
					//
					//	build the path to each entry - if it is a subfolder, open it
					//

						$plugin_path = $path.$slash.$plugin_dir;

						if(is_dir($plugin_path))
						{
							$d_plugin = dir($plugin_path);

						//
						//	loop through each item in this subfolder, searching for $config_page
						//

							while(false !== ($entry = $d_plugin->read()))
							{
								if(is_file($plugin_path.$slash.$entry) && $entry === $config_page)
								{
									$tmp = $plugin_dir;
									break(2);
								}
							}
						}
					}
				}

			//
			//	if the loop finds the config file it saves the directory as $tmp and breaks the loop
			//

				if($tmp)
				{
					$dir = $tmp;
					$plugins_dir = "mu-plugins";
					$config_parent = "wpmu-admin.php";

					as_log("Using WPMU - Admin SSL directory changed to: $dir");
				}
			}

		//
		//	function returns true if WPMU has been detected
		//

			if(!defined("TEST") || $first_test):
			function is_wpmu()
			{
				global $plugins_dir;
				return($plugins_dir === "mu-plugins" ? true : false);
			}endif;

		//
		//	log variables just defined
		//

			as_log("Plugins directory: $plugins_dir
				Config page: $config_page
				Is WPMU: ".(is_wpmu() ? "Yes" : "No"));

	//
	//
	//	GET (OR SET DEFAULT) OPTIONS
	//
	//

		require_once("includes/options.php");

	//
	//
	//	THIS IS WHERE THE REAL STUFF BEGINS...
	//
	//

		if(!defined("TEST") || $first_test):

		//
		//
		//	WORDPRESS HOOKS - CHECKING HTTP/HTTPS
		//
		//

			require_once("includes/https.php");

		//
		//
		//	WORDPRESS HOOKS - CONFIGURATION
		//
		//

			require_once("includes/config.php");

		//
		//
		//	WORDPRESS HOOKS - COOKIE HANDLING
		//
		//

			require_once("includes/cookies.php");

		//
		//
		//	ADD WORDPRESS HOOKS
		//
		//

			require_once("includes/hooks.php");

		//
		//
		//	REPLACE WP 2.6 COOKIE CODE
		//
		//

			require_once("includes/wp-2.5-auth-cookies.php");

		endif;
	}

?>